package com.example.chatplatform.mapper;

import com.example.chatplatform.model.dto.CreateGroupDTO;
import com.example.chatplatform.model.dto.GroupAnmDTO;
import com.example.chatplatform.model.dto.GroupManagerDTO;
import com.example.chatplatform.model.vo.GroupAnmVO;
import com.example.chatplatform.model.vo.GroupDetailVO;
import com.example.chatplatform.model.vo.GroupInformVO;
import com.example.chatplatform.model.vo.GroupMemberVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface GroupMapper {

    @Select("select user.user_id, user.user_account, user_name, user_avatar, user_job from group_user" +
            " join user on user.user_id = group_user.user_id " +
            "where group_user.group_id = #{groupId}")
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "userAccount", column = "user_account"),
            @Result(property = "userName", column = "user_name"),
            @Result(property = "userAvatar", column = "user_avatar"),
            @Result(property = "userJob", column = "user_job")
    })
    List<GroupMemberVO> groupMember(Integer groupId);

    @Select("select group_anm.group_anm_id, group_anm.group_id, group_anm.user_id, user.user_name, group_anm.group_anm, group_anm.group_anm_time from group_anm" +
            " join user on user.user_id = group_anm.user_id " +
            "where group_anm.group_id = #{groupId}")
    @Results({
            @Result(property = "groupAnmId", column = "group_anm_id"),
            @Result(property = "groupId", column = "group_id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "userName", column = "user_name"),
            @Result(property = "groupAnm", column = "group_anm"),
            @Result(property = "groupAnmTime", column = "group_anm_time")
    })
    List<GroupAnmVO> groupAnm(Integer groupId);

    @Insert("insert into group_anm(group_id, user_id, group_anm, group_anm_time) " +
            "values (#{groupId}, #{userId}, #{groupAnm}, #{groupAnmTime})")
    int addGroupAnm(GroupAnmDTO groupAnmDTO);

    @Update("update group_user set user_job = 2 where group_id = #{groupId} and user_id = #{userId}")
    Integer setGroupManager(GroupManagerDTO groupManagerDTO);

    @Insert("insert into group_inform(user_id, group_id, handle_id, handle_message, handle_time) " +
            "values (#{groupManagerDTO.userId}, #{groupManagerDTO.groupId}, #{groupManagerDTO.handleId}," +
            " #{handleMessage}, #{formattedDateTime})")
    Integer addGroupInform(GroupManagerDTO groupManagerDTO, String handleMessage, String formattedDateTime);

    @Update("update group_user set user_job = 1 where group_id = #{groupId} and user_id = #{userId}")
    Integer cancelGroupManager(GroupManagerDTO groupManagerDTO);

    @Delete("delete from group_user where group_id = #{groupId} and user_id = #{userId}")
    Integer removeMember(GroupManagerDTO groupManagerDTO);

    @Select("select group_inform_id, group_inform.group_id, group_avatar, group_name," +
            " group_inform.user_id, user_name, handle_message, handle_time from group_inform " +
            "join user on user.user_id = group_inform.handle_id " +
            "join `group` on `group`.group_id = group_inform.group_id " +
            "where group_inform.user_id = #{userId}")
    @Results({
            @Result(property = "groupInformId", column = "group_inform_id"),
            @Result(property = "groupId", column = "group_id"),
            @Result(property = "groupAvatar", column = "group_avatar"),
            @Result(property = "groupName", column = "group_name"),
            @Result(property = "handleId", column = "user_id"),
            @Result(property = "handleName", column = "user_name"),
            @Result(property = "handleMessage", column = "handle_message"),
            @Result(property = "handleTime", column = "handle_time")
    })
    List<GroupInformVO> getGroupInform(Integer userId);

    @Select("select group_id, group_account, group_name, group_avatar, group_number, group_intro, group_time from `group` where group_id = #{groupId}")
    @Results({
            @Result(property = "groupId", column = "group_id"),
            @Result(property = "groupAccount", column = "group_account"),
            @Result(property = "groupName", column = "group_name"),
            @Result(property = "groupAvatar", column = "group_avatar"),
            @Result(property = "groupNumber", column = "group_number"),
            @Result(property = "groupIntro", column = "group_intro"),
            @Result(property = "groupTime", column = "group_time")
    })
    GroupDetailVO getGroupInfo(Integer groupId);

    @Select("select group_user_id from group_user where group_id = #{groupId} and user_id = #{userId}")
    Integer ifUserGroup(Integer userId, Integer groupId);

    @Select("select user_job from group_user where group_id = #{groupId} and user_id = #{userId}")
    int ifGroup(Integer userId, Integer groupId);

    @Insert("insert into group_user(group_id, user_id, user_job) " +
            "values (#{groupId}, #{userId}, 1)")
    Integer joinGroup(GroupManagerDTO groupManagerDTO);

    @Select("select COUNT(group_user_id) from group_user where group_id = #{groupId}")
    int groupNumber(Integer groupId);

    @Update("update `group` set group_number = #{i} where group_id = #{groupId}")
    void updateGroupNumber(int groupId, int i);

    @Delete("delete from group_user where group_id = #{groupId} and user_id = #{userId}")
    Integer quitGroup(GroupManagerDTO groupManagerDTO);

    @Select("select group_id from `group` where group_account = #{groupAccount}")
    Integer groupAccount(CreateGroupDTO createGroupDTO);

    @Insert("insert into `group`(group_account, group_name, group_avatar, group_number, group_intro, group_time) " +
            "values (#{createGroupDTO.groupAccount}, #{createGroupDTO.groupName}, #{fileUrl}, 1, #{createGroupDTO.groupIntro}, #{createGroupDTO.groupTime})")
    @Options(useGeneratedKeys = true, keyProperty = "createGroupDTO.groupId")
    Integer createGroup(CreateGroupDTO createGroupDTO, String fileUrl);

    @Insert("insert into group_user(group_id, user_id, user_job) " +
            "values (#{groupId}, #{userId}, 3)")
    void addGroupUser(CreateGroupDTO createGroupDTO);

    @Select("select user_id from group_user where group_id = #{groupId}")
    List<Integer> groupUserId(GroupManagerDTO groupManagerDTO);

    @Delete("delete from group_message where group_id = #{groupId}")
    void removeMessage(GroupManagerDTO groupManagerDTO);

    @Insert("insert into delete_group(group_id) " +
            "values (#{groupId})")
    int disbandGroup(GroupManagerDTO groupManagerDTO);

}
